package com.noshufou.android.su.provider;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import com.noshufou.android.su.util.Util;
import java.util.HashMap;
import java.util.List;

public class PermissionsProvider extends ContentProvider
{
  private static final int APPS = 100;
  private static final int APP_CLEAN = 107;
  private static final int APP_COUNT = 105;
  private static final int APP_COUNT_TYPE = 106;
  private static final int APP_ID = 101;
  private static final int APP_ID_LOGS = 102;
  private static final int APP_UID = 103;
  private static final int APP_UID_LOGS = 104;
  public static final String AUTHORITY = "com.noshufou.android.su.provider";
  private static final int LOGS = 200;
  private static final int LOGS_APP_ID = 202;
  private static final int LOGS_TYPE = 203;
  private static final String TAG = "Su.PermissionsProvider";
  private static final HashMap<String, String> sAppsProjectionMap;
  private static final HashMap<String, String> sLogsProjectionMap;
  private static final UriMatcher sUriMatcher = new UriMatcher(-1);
  private Context mContext;
  private SQLiteDatabase mDb = null;
  private SuDbOpenHelper mDbHelper = null;

  static
  {
    sUriMatcher.addURI("com.noshufou.android.su.provider", "apps", 100);
    sUriMatcher.addURI("com.noshufou.android.su.provider", "apps/#", 101);
    sUriMatcher.addURI("com.noshufou.android.su.provider", "apps/clean", 107);
    sUriMatcher.addURI("com.noshufou.android.su.provider", "apps/#/logs", 102);
    sUriMatcher.addURI("com.noshufou.android.su.provider", "apps/uid/#", 103);
    sUriMatcher.addURI("com.noshufou.android.su.provider", "apps/uid/#/logs", 104);
    sUriMatcher.addURI("com.noshufou.android.su.provider", "apps/count", 105);
    sUriMatcher.addURI("com.noshufou.android.su.provider", "apps/count/#", 106);
    sUriMatcher.addURI("com.noshufou.android.su.provider", "logs", 200);
    sUriMatcher.addURI("com.noshufou.android.su.provider", "logs/#", 202);
    sUriMatcher.addURI("com.noshufou.android.su.provider", "logs/type/#", 203);
    sAppsProjectionMap = new HashMap();
    sAppsProjectionMap.put("_id", "apps._id AS _id");
    sAppsProjectionMap.put("uid", "apps.uid");
    sAppsProjectionMap.put("package", "apps.package");
    sAppsProjectionMap.put("name", "apps.name");
    sAppsProjectionMap.put("exec_uid", "apps.exec_uid");
    sAppsProjectionMap.put("exec_cmd", "apps.exec_cmd");
    sAppsProjectionMap.put("allow", "apps.allow");
    sAppsProjectionMap.put("date", "logs.date");
    sAppsProjectionMap.put("type", "logs.type");
    sAppsProjectionMap.put("notifications", "apps.notifications");
    sAppsProjectionMap.put("logging", "apps.logging");
    sAppsProjectionMap.put("dirty", "apps.dirty");
    sLogsProjectionMap = new HashMap();
    sLogsProjectionMap.put("_id", "logs._id AS _id");
    sLogsProjectionMap.put("app_id", "logs.app_id");
    sLogsProjectionMap.put("uid", "apps.uid");
    sLogsProjectionMap.put("name", "apps.name");
    sLogsProjectionMap.put("package", "apps.package");
    sLogsProjectionMap.put("date", "logs.date");
    sLogsProjectionMap.put("type", "logs.type");
  }

  private boolean ensureDb()
  {
    if (this.mDb == null)
    {
      this.mDb = this.mDbHelper.getWritableDatabase();
      if (this.mDb != null);
    }
    for (int i = 0; ; i = 1)
      return i;
  }

  public int delete(Uri paramUri, String paramString, String[] paramArrayOfString)
  {
    int j;
    if (!ensureDb())
    {
      j = -1;
      return j;
    }
    int i = 0;
    ContentValues localContentValues = new ContentValues();
    localContentValues.put("allow", Integer.valueOf(-2));
    localContentValues.put("dirty", Integer.valueOf(1));
    switch (sUriMatcher.match(paramUri))
    {
    default:
      throw new IllegalArgumentException("Unsupported URI: " + paramUri);
    case 100:
      j = this.mDb.update("apps", localContentValues, paramString, paramArrayOfString);
      Util.updatePermissionsDb(this.mContext);
    case 101:
    case 102:
    case 202:
    case 103:
    case 107:
    case 200:
    }
    while (true)
    {
      getContext().getContentResolver().notifyChange(paramUri, null);
      getContext().getContentResolver().notifyChange(Apps.CONTENT_URI, null);
      break;
      SQLiteDatabase localSQLiteDatabase3 = this.mDb;
      StringBuilder localStringBuilder3 = new StringBuilder("_id=").append((String)paramUri.getPathSegments().get(1));
      String str3;
      label264: SQLiteDatabase localSQLiteDatabase2;
      StringBuilder localStringBuilder2;
      if (!TextUtils.isEmpty(paramString))
      {
        str3 = " AND (" + paramString + ")";
        i = localSQLiteDatabase3.update("apps", localContentValues, str3, paramArrayOfString);
        Util.updatePermissionsDb(this.mContext);
        localSQLiteDatabase2 = this.mDb;
        localStringBuilder2 = new StringBuilder("app_id=").append((String)paramUri.getPathSegments().get(1));
        if (TextUtils.isEmpty(paramString))
          break label393;
      }
      label393: for (String str2 = " AND (" + paramString + ")"; ; str2 = "")
      {
        j = i + localSQLiteDatabase2.delete("logs", str2, paramArrayOfString);
        break;
        str3 = "";
        break label264;
      }
      SQLiteDatabase localSQLiteDatabase1 = this.mDb;
      StringBuilder localStringBuilder1 = new StringBuilder("uid=").append((String)paramUri.getPathSegments().get(2));
      if (!TextUtils.isEmpty(paramString));
      for (String str1 = " AND (" + paramString + ")"; ; str1 = "")
      {
        j = localSQLiteDatabase1.update("apps", localContentValues, str1, paramArrayOfString);
        Util.updatePermissionsDb(this.mContext);
        break;
      }
      j = this.mDb.delete("apps", paramString, paramArrayOfString);
      continue;
      j = this.mDb.delete("logs", paramString, paramArrayOfString);
    }
  }

  public String getType(Uri paramUri)
  {
    String str;
    switch (sUriMatcher.match(paramUri))
    {
    default:
      throw new IllegalArgumentException("Unsupported URI: " + paramUri);
    case 100:
      str = "vnd.android.cursor.dir/vnd.noshufou.superuser.apps ";
    case 101:
    case 103:
    case 105:
    case 106:
    case 102:
    case 104:
    case 200:
    case 202:
    case 203:
    }
    while (true)
    {
      return str;
      str = "vnd.android.cursor.item/vnd.noshufou.superuser.apps ";
      continue;
      str = "vnd.android.cursor.dir/vnd.noshufou.superuser.logs ";
    }
  }

  public Uri insert(Uri paramUri, ContentValues paramContentValues)
  {
    Uri localUri;
    if (!ensureDb())
    {
      localUri = null;
      return localUri;
    }
    long l1 = 0L;
    switch (sUriMatcher.match(paramUri))
    {
    default:
      throw new IllegalArgumentException("Unsupported URI: " + paramUri);
    case 100:
      paramContentValues.put("dirty", "1");
    case 102:
    case 202:
    }
    while (true)
    {
      try
      {
        long l2 = this.mDb.insertOrThrow("apps", null, paramContentValues);
        l1 = l2;
        if (paramContentValues.getAsInteger("allow").intValue() == -1)
          continue;
        ContentValues localContentValues = new ContentValues();
        localContentValues.put("app_id", Long.valueOf(l1));
        localContentValues.put("date", Long.valueOf(System.currentTimeMillis()));
        localContentValues.put("type", Integer.valueOf(2));
        this.mDb.insert("logs", null, localContentValues);
        Util.updatePermissionsDb(this.mContext);
        localUri = ContentUris.withAppendedId(Apps.CONTENT_URI, l1);
        if (l1 <= -1L)
          break label426;
        getContext().getContentResolver().notifyChange(localUri, null);
      }
      catch (SQLException localSQLException)
      {
        String[] arrayOfString1 = new String[3];
        arrayOfString1[0] = paramContentValues.getAsString("uid");
        arrayOfString1[1] = paramContentValues.getAsString("exec_uid");
        arrayOfString1[2] = paramContentValues.getAsString("exec_cmd");
        this.mDb.update("apps", paramContentValues, "uid=? AND exec_uid=? AND exec_cmd=?", arrayOfString1);
        SQLiteDatabase localSQLiteDatabase = this.mDb;
        String[] arrayOfString2 = new String[1];
        arrayOfString2[0] = "_id";
        Cursor localCursor = localSQLiteDatabase.query("apps", arrayOfString2, "uid=? AND exec_uid=? AND exec_cmd=?", arrayOfString1, null, null, null);
        if (!localCursor.moveToFirst())
          continue;
        l1 = localCursor.getLong(0);
        localCursor.close();
        continue;
      }
      paramContentValues.put("app_id", (String)paramUri.getPathSegments().get(1));
      l1 = this.mDb.insert("logs", null, paramContentValues);
      localUri = ContentUris.withAppendedId(Logs.CONTENT_URI, l1);
      getContext().getContentResolver().notifyChange(ContentUris.withAppendedId(Logs.CONTENT_URI, Long.parseLong((String)paramUri.getPathSegments().get(1))), null);
      getContext().getContentResolver().notifyChange(Apps.CONTENT_URI, null);
    }
    label426: throw new SQLException("Failed to insert row into " + paramUri);
  }

  public boolean onCreate()
  {
    this.mContext = getContext();
    this.mDbHelper = new SuDbOpenHelper(this.mContext);
    if (this.mDbHelper == null);
    for (int i = 0; ; i = 1)
      return i;
  }

  public Cursor query(Uri paramUri, String[] paramArrayOfString1, String paramString1, String[] paramArrayOfString2, String paramString2)
  {
    Object localObject;
    if (!ensureDb())
      localObject = null;
    while (true)
    {
      return localObject;
      SQLiteQueryBuilder localSQLiteQueryBuilder = new SQLiteQueryBuilder();
      ((String[])null);
      String str1 = null;
      int i = sUriMatcher.match(paramUri);
      String[] arrayOfString;
      String str2;
      switch (i)
      {
      default:
        throw new IllegalArgumentException("Unsupported URI: " + paramUri);
      case 100:
      case 101:
      case 103:
        localSQLiteQueryBuilder.setTables("apps LEFT OUTER JOIN logs ON apps._id=logs.app_id");
        localSQLiteQueryBuilder.setProjectionMap(sAppsProjectionMap);
        arrayOfString = Apps.DEFAULT_PROJECTION;
        str1 = "apps._id";
        if (paramString2 == null)
          paramString2 = "apps.allow DESC, apps.name ASC";
        StringBuilder localStringBuilder = new StringBuilder(String.valueOf(paramString2));
        if (TextUtils.isEmpty(paramString2))
          break;
        str2 = ", ";
        label219: paramString2 = localStringBuilder.append(str2).toString() + "date" + " DESC";
        switch (i)
        {
        default:
          label255: if (paramArrayOfString1 == null)
            paramArrayOfString1 = arrayOfString;
          label324: localObject = null;
        case 101:
        case 102:
        case 202:
        case 103:
        case 104:
        case 203:
        case 106:
        }
      case 102:
      case 104:
      case 200:
      case 202:
      case 203:
      case 105:
      case 106:
      }
      try
      {
        Cursor localCursor = localSQLiteQueryBuilder.query(this.mDb, paramArrayOfString1, paramString1, paramArrayOfString2, str1, null, paramString2);
        localObject = localCursor;
        localObject.setNotificationUri(this.mContext.getContentResolver(), paramUri);
        continue;
        str2 = "";
        break label219;
        localSQLiteQueryBuilder.setTables("logs LEFT OUTER JOIN apps ON logs.app_id=apps._id");
        localSQLiteQueryBuilder.setProjectionMap(sLogsProjectionMap);
        arrayOfString = Logs.DEFAULT_PROJECTION;
        if (paramString2 == null)
          paramString2 = "logs.date DESC";
        break label255;
        localSQLiteQueryBuilder.setTables("apps");
        arrayOfString = new String[1];
        arrayOfString[0] = "COUNT() as rows";
        break label255;
        localSQLiteQueryBuilder.appendWhere(" apps._id=" + (String)paramUri.getPathSegments().get(1));
        break label324;
        localSQLiteQueryBuilder.appendWhere("apps._id=" + (String)paramUri.getPathSegments().get(1));
        break label324;
        localSQLiteQueryBuilder.appendWhere(" apps.uid=" + (String)paramUri.getPathSegments().get(2));
        break label324;
        localSQLiteQueryBuilder.appendWhere("apps.uid=" + (String)paramUri.getPathSegments().get(2));
        break label324;
        localSQLiteQueryBuilder.appendWhere("logs.type=" + (String)paramUri.getPathSegments().get(2));
        break label324;
        localSQLiteQueryBuilder.appendWhere("apps.allow=" + (String)paramUri.getPathSegments().get(2));
      }
      catch (SQLiteException localSQLiteException)
      {
        while (true)
          Log.e("Su.PermissionsProvider", "Query failed, returning null cursor.", localSQLiteException);
      }
    }
  }

  public int update(Uri paramUri, ContentValues paramContentValues, String paramString, String[] paramArrayOfString)
  {
    int j;
    if (!ensureDb())
      j = -1;
    while (true)
    {
      return j;
      int i = 0;
      if (!paramContentValues.containsKey("dirty"))
      {
        Log.d("Su.PermissionsProvider", "Row dirty, update permissions.sqlite");
        paramContentValues.put("dirty", "1");
        i = 1;
      }
      while (true)
        switch (sUriMatcher.match(paramUri))
        {
        case 102:
        default:
          throw new IllegalArgumentException("Unsupported URI: " + paramUri);
          Log.d("Su.PermissionsProvider", "Row not dirty, don't update permissions.sqlite");
        case 100:
        case 101:
        case 103:
        }
      j = this.mDb.update("apps", paramContentValues, paramString, paramArrayOfString);
      if (i != 0)
        Util.updatePermissionsDb(this.mContext);
      getContext().getContentResolver().notifyChange(paramUri, null);
    }
    SQLiteDatabase localSQLiteDatabase2 = this.mDb;
    StringBuilder localStringBuilder2 = new StringBuilder("_id=").append((String)paramUri.getPathSegments().get(1));
    if (!TextUtils.isEmpty(paramString));
    for (String str2 = " AND (" + paramString + ")"; ; str2 = "")
    {
      j = localSQLiteDatabase2.update("apps", paramContentValues, str2, paramArrayOfString);
      break;
    }
    SQLiteDatabase localSQLiteDatabase1 = this.mDb;
    StringBuilder localStringBuilder1 = new StringBuilder("uid=").append((String)paramUri.getPathSegments().get(2));
    if (!TextUtils.isEmpty(paramString));
    for (String str1 = " AND (" + paramString + ")"; ; str1 = "")
    {
      j = localSQLiteDatabase1.update("apps", paramContentValues, str1, paramArrayOfString);
      break;
    }
  }

  public static class Apps
  {
    public static final String ALLOW = "allow";
    public static final String APPS_LOGS_JOIN = "apps LEFT OUTER JOIN logs ON apps._id=logs.app_id";
    public static final Uri CONTENT_URI = Uri.parse("content://com.noshufou.android.su.provider/apps");
    public static final Uri COUNT_CONTENT_URI = Uri.parse("content://com.noshufou.android.su.provider/apps/count");
    public static final String CREATE = "CREATE TABLE IF NOT EXISTS apps (_id INTEGER PRIMARY KEY AUTOINCREMENT, uid INTEGER, package TEXT, name TEXT,  exec_uid INTEGER, exec_cmd TEXT, allow INTEGER, notifications INTEGER, logging INTEGER, dirty INTEGER, UNIQUE (uid,exec_uid,exec_cmd));";
    public static final String[] DEFAULT_PROJECTION;
    public static final String DEFAULT_SORT_ORDER = "apps.allow DESC, apps.name ASC";
    public static final String DIRTY = "dirty";
    public static final String EXEC_CMD = "exec_cmd";
    public static final String EXEC_UID = "exec_uid";
    public static final String LAST_ACCESS = "date";
    public static final String LAST_ACCESS_TYPE = "type";
    public static final String LOGGING = "logging";
    public static final String NAME = "name";
    public static final String NOTIFICATIONS = "notifications";
    public static final String PACKAGE = "package";
    public static final String TABLE_NAME = "apps";
    public static final String UID = "uid";
    public static final String _ID = "_id";

    static
    {
      String[] arrayOfString = new String[11];
      arrayOfString[0] = "_id";
      arrayOfString[1] = "uid";
      arrayOfString[2] = "package";
      arrayOfString[3] = "name";
      arrayOfString[4] = "exec_uid";
      arrayOfString[5] = "exec_cmd";
      arrayOfString[6] = "allow";
      arrayOfString[7] = "date";
      arrayOfString[8] = "type";
      arrayOfString[9] = "notifications";
      arrayOfString[10] = "logging";
      DEFAULT_PROJECTION = arrayOfString;
    }

    public static final class AllowType
    {
      public static final int ALLOW = 1;
      public static final int ASK = -1;
      public static final int DENY = 0;
      public static final int TO_DELETE = -2;
    }
  }

  public static class Logs
  {
    public static final String APP_ID = "app_id";
    public static final Uri CONTENT_URI = Uri.parse("content://com.noshufou.android.su.provider/logs");
    public static final String CREATE = "CREATE TABLE IF NOT EXISTS logs (_id INTEGER PRIMARY KEY AUTOINCREMENT, app_id INTEGER, date INTEGER, type INTEGER);";
    public static final String DATE = "date";
    public static final String[] DEFAULT_PROJECTION;
    public static final String DEFAULT_SORT_ORDER = "logs.date DESC";
    public static final String LOGS_APPS_JOIN = "logs LEFT OUTER JOIN apps ON logs.app_id=apps._id";
    public static final String NAME = "name";
    public static final String PACKAGE = "package";
    public static final String TABLE_NAME = "logs";
    public static final String TYPE = "type";
    public static final String UID = "uid";
    public static final String _ID = "_id";

    static
    {
      String[] arrayOfString = new String[7];
      arrayOfString[0] = "_id";
      arrayOfString[1] = "app_id";
      arrayOfString[2] = "uid";
      arrayOfString[3] = "name";
      arrayOfString[4] = "package";
      arrayOfString[5] = "date";
      arrayOfString[6] = "type";
      DEFAULT_PROJECTION = arrayOfString;
    }

    public static final class LogType
    {
      public static final int ALLOW = 1;
      public static final int CREATE = 2;
      public static final int DENY = 0;
      public static final int TOGGLE = 3;
    }
  }

  private class SuDbOpenHelper extends SQLiteOpenHelper
  {
    private static final String DATABASE_NAME = "su.db";
    private static final int DATABASE_VERSION = 4;

    SuDbOpenHelper(Context arg2)
    {
      super("su.db", null, 4);
    }

    public void onCreate(SQLiteDatabase paramSQLiteDatabase)
    {
      paramSQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS apps (_id INTEGER PRIMARY KEY AUTOINCREMENT, uid INTEGER, package TEXT, name TEXT,  exec_uid INTEGER, exec_cmd TEXT, allow INTEGER, notifications INTEGER, logging INTEGER, dirty INTEGER, UNIQUE (uid,exec_uid,exec_cmd));");
      paramSQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS logs (_id INTEGER PRIMARY KEY AUTOINCREMENT, app_id INTEGER, date INTEGER, type INTEGER);");
    }

    public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2)
    {
      int i = paramInt1;
      if (i == 1)
        i = 2;
      if (i == 2);
      try
      {
        paramSQLiteDatabase.execSQL("ALTER TABLE apps ADD COLUMN dirty INTEGER");
        ContentValues localContentValues2 = new ContentValues();
        localContentValues2.put("dirty", "1");
        paramSQLiteDatabase.update("apps", localContentValues2, null, null);
        Util.updatePermissionsDb(PermissionsProvider.this.mContext);
        i = 3;
        if (i == 3)
        {
          String[] arrayOfString1 = new String[3];
          arrayOfString1[0] = "_id";
          arrayOfString1[1] = "uid";
          arrayOfString1[2] = "name";
          localCursor = paramSQLiteDatabase.query("apps", arrayOfString1, null, null, null, null, null);
          if (!localCursor.moveToNext())
            localCursor.close();
        }
        else
        {
          return;
        }
      }
      catch (SQLiteException localSQLiteException)
      {
        while (true)
        {
          Cursor localCursor;
          Log.e("Su.PermissionsProvider", "dirty column already exists... wut?", localSQLiteException);
          continue;
          if (!localCursor.getString(2).equalsIgnoreCase("unknown"))
            continue;
          ContentValues localContentValues1 = new ContentValues();
          localContentValues1.put("name", Util.getAppName(PermissionsProvider.this.mContext, localCursor.getInt(1), false));
          localContentValues1.put("package", Util.getAppPackage(PermissionsProvider.this.mContext, localCursor.getInt(1)));
          String[] arrayOfString2 = new String[1];
          arrayOfString2[0] = String.valueOf(localCursor.getLong(0));
          paramSQLiteDatabase.update("apps", localContentValues1, "_id=?", arrayOfString2);
        }
      }
    }
  }
}

/* Location:           D:\Development\Decompile_Workspace\슈퍼유저-41\classes_dex2jar.jar
 * Qualified Name:     com.noshufou.android.su.provider.PermissionsProvider
 * JD-Core Version:    0.6.0
 */